package controller

import (
	"commons/entity"
	"commons/enums/sysCode"
	"commons/handler/request"
	"commons/handler/response"
	"commons/output/jsonError"
	"commons/record"
	"commons/util/tokenUtil"
	"imi/model/userModel"
	"models"
)

type BaseController struct {
	token    string
	user     models.CmfUser
	userInfo entity.UserInfo
	Init     func()
}

var Allow = []string{"/user/login"}

func (b *BaseController) Init_() {

}

func (b *BaseController) Auth() (bool bool, res response.Handle) {
	var err error
	token := request.Request.Header["Token"]
	if token == nil {
		record.LogError("token参数不存在")
		bool = false
		res = jsonError.Write(sysCode.Code.ERROR, "token不存在", nil)
		return
	}
	b.token = token[0]
	b.userInfo, err = tokenUtil.Parse(b.token)
	if err != nil {
		bool = false
		res = jsonError.Write(sysCode.Code.ERROR, err.Error(), nil)
		return
	}
	b.user, err = userModel.GetByUserID(b.userInfo.Id)
	b.user.UserPass = ""
	if err != nil {
		record.LogError("用户不存在")
		bool = false
		res = jsonError.Write(sysCode.Code.ERROR, "用户不存在", nil)
		return
	}
	bool = true
	res = jsonError.Write(sysCode.Code.ERROR, "成功", nil)
	return
}
func (b *BaseController) getToken() {
	_ = request.Request.Header["Token"]
}
